package com.example.controller;

import com.example.pojo.Content;
import com.example.pojo.PageData;
import com.example.pojo.ResultBean;
import com.example.service.ContentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/content")
public class ContentController {
    @Autowired
    private ContentService contentService;

    @GetMapping("/list")
    public ResultBean list(PageData pageData) {
        PageHelper.startPage(pageData.getPageNum(), pageData.getPageSize());
        List<Content> contents = contentService.queryAll();
        return ResultBean.success(new PageInfo(contents));
    }

    @GetMapping("/queryAllContentCategory")
    public ResultBean queryAllContentCategory() {
        return ResultBean.success(contentService.queryAllContentCategory());
    }

    @PostMapping("/saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody Content content) {
        System.out.println("新增或更新的数据：" + content);
        int rs = contentService.saveOrUpdate(content);
        return rs >= 1 ? ResultBean.success() : ResultBean.error();
    }

    @DeleteMapping("/delete/{id}")
    public ResultBean delete(@PathVariable("id") Long id) {
        System.out.println("删除广告的id：" + id);
        int rs = contentService.deleteById(id);
        return rs >= 1 ? ResultBean.success() : ResultBean.error();
    }

    @GetMapping("/queryContentByCategoryId/{categoryId}")
    public ResultBean queryContentByCategoryId(@PathVariable("categoryId") Long categoryId) {
        System.out.println("请求的分类Id：" + categoryId);
        List<Content> contents = contentService.queryContentByCategoryId(categoryId);
        return ResultBean.success(contents);
    }
}
